package entity;

import item.Requirements;
import item.TakeableItem;
import loadsave.Memento;
import loadsave.loadsaveobject.Iterator;
import loadsave.loadsaveobject.LoadSaveObject;
import loadsave.loadsaveobject.LoadSaveObjectComposite;
import occupation.Occupation;

public class Avatar extends Entity {

	private Quest completedEvents;
	
	public Avatar() {
		completedEvents = new Quest();
	}
	
	public Avatar(String modelName, Occupation occ) {
		super(modelName, occ);
//		completedEvents = new ArrayList<Event>();
	}
	
	public void completeEvent(Event event) {
		completedEvents.completeEvent(event);
	}
	
	/**
	 * It compares the required object (items,occupation, etc) against the Avatar's
	 * @param Requirements req
	 * @return boolean
	 */
	@Override
	public boolean checkReqs(Requirements req) {
		if(!req.getOccType().contains(occupation)) {
			return false;
		}
		for(TakeableItem item : req.getReqdItems()) {
			if(!inventory.contains(item))
				return false;
		}
		if(stats.compare(req.getMinStats()) == -1 ) {
			return false;
		}
		for(String eventID : req.getReqdTriggers()) {
			Event event = new Event(eventID);
			if(!completedEvents.hasCompleted(event))
				return false;
		}
		return true;
	}

	@Override
	public Memento getMemento() {
		return new Memento() {

			@Override
			public LoadSaveObject getState() {
				Iterator<LoadSaveObject> i = Avatar.super.getMemento().getState().getIterator();
				LoadSaveObjectComposite toReturn = new LoadSaveObjectComposite("", "Avatar");
				for (i.init(); i.hasNext(); i.next()) {
					toReturn.add(i.getCurrKey().getValue(), i.getCurrVal());
				}
				LoadSaveObject lso = completedEvents.getMemento().getState();
				lso.setName("completedEvents");
				toReturn.add(lso.getName(), lso);
				return toReturn;
			}

			@Override
			public void restoreState(LoadSaveObject lso) {
				Avatar.super.getMemento().restoreState(lso);
			}
			
		};
	}
	
}
